package com.naizhuai.ui.testunit;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.adapter.business.ActionAddImageAdapterHolder;
import com.naizhuai.ui.entity.QRCodeMsgBean;
import com.naizhuai.ui.entity.QRCodeScanBean;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.ui.entity.mainbusiness.ActionSelectedProcessImageDetails;
import com.naizhuai.util.BitmapUtils;
import com.naizhuai.util.DeviceUtils;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.QRCodeUtil;
import com.naizhuai.util.RxBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;

/**
 * 生成的二维码展示界面
 */
public class ManageTestCreateQRCodeActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "ManageTestCreateQRCodeActivity";

    @BindView(R.id.image_qr_code)
    ImageView qrCodeImage;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_thumb)
    ImageView userThumb;

    @BindView(R.id.qr_scan_txt)
    TextView scanTxt;

    @BindView(R.id.bar_top)
    View barTop;

    private QRCodeScanBean qrCodeScanBean;

    private QRCodeMsgBean qrCodeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBaseData();
        initBaseView();
    }

    private void initBaseData() {
        qrCodeScanBean = null;
        qrCodeBean = (QRCodeMsgBean) getIntent().getSerializableExtra(QRCodeMsgBean.QRCode_MSG_KEY);
    }

    private void initBaseView() {
        qrCodeImage.post(new Runnable() {
            @Override
            public void run() {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) qrCodeImage.getLayoutParams();
                int width = qrCodeImage.getWidth();
                params.height = width;
                qrCodeImage.setLayoutParams(params);
                if (qrCodeBean != null)
                    initView(width);
                else {
                    shortToast(getText(R.string.qrcode_error).toString());
                    onBackPressed();
                }
            }
        });
    }

    private void initView(int size) {
        String logoUrl = "";
        String name = "";
        qrCodeScanBean = new QRCodeScanBean();
        qrCodeScanBean.setJoinType(qrCodeBean.getScanType());
        switch (qrCodeBean.getScanType()) {
            case QRCodeMsgBean.USER_JOIN_TYPE:
                scanTxt.setText(getString(R.string.qr_code_for_user));
                logoUrl = qrCodeBean.getPerson().getUserThumb();
                name = qrCodeBean.getPerson().getUserName();
                qrCodeScanBean.setUserId(qrCodeBean.getPerson().getUserId());
                userThumb.setVisibility(View.VISIBLE);
                barTop.setVisibility(View.VISIBLE);
                ImageUtils.glideThumbViewByUrl(this,userThumb,logoUrl);
                new BitmapThread(logoUrl).start();
                break;
            case QRCodeMsgBean.DEPARTMENT_JOIN_TYPE:
                scanTxt.setText(getString(R.string.qr_code_for_group));
                userThumb.setVisibility(View.GONE);
                barTop.setVisibility(View.INVISIBLE);
                name = qrCodeBean.getGroup().getDepartName();
                qrCodeScanBean.setCompanyId(qrCodeBean.getGroup().getCompanyId());
                qrCodeScanBean.setDepartmentId(qrCodeBean.getGroup().getDepartId());
                qrCodeImage.setImageBitmap(QRCodeUtil.createQRCodeBitmap(qrCodeScanBean.getScanEnMsg(), size, size));
                break;
            case QRCodeMsgBean.COMPANY_JOIN_TYPE:
                scanTxt.setText(getString(R.string.qr_code_for_company));
                userThumb.setVisibility(View.GONE);
                barTop.setVisibility(View.INVISIBLE);
                name = qrCodeBean.getGroup().getCompanyName();
                qrCodeScanBean.setCompanyId(qrCodeBean.getGroup().getCompanyId());
                qrCodeImage.setImageBitmap(QRCodeUtil.createQRCodeBitmap(qrCodeScanBean.getScanEnMsg(), size, size));
                break;
        }
        urlHandler = new UrlHandler(qrCodeImage, qrCodeScanBean, size, this);

        userName.setText(name);
    }

    @OnClick({R.id.title_delete_icon})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_delete_icon:
                super.onBackPressed();
                break;
        }
    }

    public class BitmapThread extends Thread {
        private String url;

        public BitmapThread(String url) {
            this.url = url;
        }

        @Override
        public void run() {
            Bitmap bitLogo = BitmapUtils.returnBitMap(url);
            urlHandler.setBitmap(bitLogo);
            urlHandler.sendEmptyMessage(0);
        }
    }

    private UrlHandler urlHandler;

    static class UrlHandler extends Handler {
        private Context mContext;
        private ImageView qrCodeImage;
        private int size;
        private QRCodeScanBean qrCodeScanBean;
        private Bitmap urlBit;

        public UrlHandler(ImageView qrCodeImage, QRCodeScanBean qrCodeScanBean, int size, Context mContext) {
            this.qrCodeImage = qrCodeImage;
            this.qrCodeScanBean = qrCodeScanBean;
            this.size = size;
            this.mContext = mContext;
        }

        public void setBitmap(Bitmap bitLogo) {
            this.urlBit = bitLogo;
        }

        @Override
        public void handleMessage(Message msg) {
            Bitmap scanLogoBit = BitmapUtils.getRoundBorderBitmap(urlBit, DeviceUtils.floatDp2px(mContext, 40f), DeviceUtils.floatDp2px(mContext, 40f), DeviceUtils.dp2px(mContext, 15));
            Bitmap scanBit = QRCodeUtil.createQRCodeWithLogo(qrCodeScanBean.getScanEnMsg(), size, scanLogoBit);
            qrCodeImage.setImageBitmap(scanBit);
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.test_activity_qr_code_layout;
    }

}
